package com.sun.local.analysis;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.sun.common.constants.Constant.SSQ_FILE_PATH;

/**
 * 随机生成数据统计概率
 */
public class StatisticsProbability {

    // 生成数据路径
    private static final String READ_PATH = System.getProperty("user.dir") + File.separator + "data" +
            File.separator + "ac" + File.separator + "ssq_ac_8.text";

    private static final String ssq;
    private static final String firstNum;
    private static final String lastNum;

    static {
        // 最新一期数据
        String latestData = FileUtil.readUtf8Lines(SSQ_FILE_PATH).get(1);
        // 获取逗号减2位到加号之间的数据
        ssq = latestData.substring(latestData.indexOf(",") - 2, latestData.indexOf("+"));
        // 逗号分隔
        String[] split = ssq.split(",");
        // 第一位
        firstNum = split[0];
        // 最后一位
        lastNum = split[split.length - 1];
    }

    public static void main(String[] args) {
        random();
    }

    /**
     * 随机生成数据并保存
     */
    public static void random() {
        // 最新一期数据
        String latestData = FileUtil.readUtf8Lines(SSQ_FILE_PATH).get(0);
        String latestSsq = latestData.substring(latestData.indexOf(",") - 2, latestData.indexOf("+"));
        System.out.println("最新一期号码：" + latestSsq);

        // 读取文件数据
        List<String> acSsqList = FileUtil.readUtf8Lines(READ_PATH);
        System.out.println("所有AC值等于8的组合数量：" + acSsqList.size());

        // 过滤历史号码
        filterRepeat(acSsqList);
        System.out.println("过滤掉历史号码的组合数量：" + acSsqList.size());

        // 杀号
        System.out.println("需要过滤的号码：" + firstNum + "," + lastNum);
        filterKillNum(acSsqList);
        System.out.println("杀号后的组合数量：" + acSsqList.size());

        int forCount = 100;
        int total = 20000;
        List<Integer> countList = new ArrayList<>();
        for (int i = 0; i < forCount; i++) {
            int count = 0;
            boolean flag = false;
            while (!flag) {
                // 对列表进行随机排序
                Collections.shuffle(acSsqList);
                count ++;

                // 获取前N个元素
                List<String> tempList = acSsqList.subList(0, total);

                for (String ssq : tempList) {
                    if(ssq.equals(latestSsq)) {
                        flag = true;
                        break;
                    }
                }
            }
            countList.add(count);
            System.out.println("count = " + count);
        }

        int oneCount = 0;
        for (Integer c : countList) {
            if(c == 1) {
                oneCount ++;
            }
        }
        System.out.printf("样本随机次数为：%d, 样本数量为：%d, 概率为：%d/100", forCount, total, oneCount);
        System.out.println("程序执行结束！");
    }

    /**
     * 过滤历史出现过的号码
     *
     * @param acSsqList 全部红球组合
     */
    private static void filterRepeat(List<String> acSsqList) {
        List<String> lines = acSsqList.stream()
                .filter(line -> !line.contains("#"))
                .collect(Collectors.toList());
        acSsqList.clear();
        acSsqList.addAll(lines);
    }

    /**
     * 排除号码
     *
     * @param acSsqList 全部红球组合
     */
    private static void filterKillNum(List<String> acSsqList) {
        List<String> lines = new ArrayList<>();
        for (String ssq : acSsqList) {
            if(!StringUtils.contains(ssq, firstNum) && !StringUtils.contains(ssq, lastNum)) {
                lines.add(ssq);
            }
        }
        acSsqList.clear();
        acSsqList.addAll(lines);
    }

}
